<template>
    <el-dialog
            :title="dialogParams.title"
            :visible.sync="dialogParams.visible"
            width="30%">
        <el-form label-width="80px" :model="formData">
            <el-form-item label="层面">
                <el-select v-model="formData.indexX" placeholder="请选择层面" @change="changeIndexX">
                    <el-option v-for="(item,index) in rangeList" :key="index" :label="item.name" :value="item.code"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="角度">
                <el-select v-model="formData.rectificationRangeId" placeholder="请选择角度" @change="changeRate">
                    <el-option v-for="(item,index) in rateList" :key="index" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="目标标准" prop="targetStandardId">
                <el-select v-model="formData.targetStandardId" placeholder="请选择目标标准" @change="changeTargetStandard">
                    <el-option v-for="(item,index) in targetStandardList" :key="index" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="质控点" prop="id">
                <el-select v-model="formData.id" placeholder="请选择质控点" @change="changePoint">
                    <el-option v-for="(item,index) in pointList" :key="index" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="标准值" prop="standardValue">
                <el-input v-model="formData.standardValue" disabled></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" @click="submit">确 定</el-button>
          </span>
    </el-dialog>
</template>

<script>
    import { queryFormWorkAll, getPointALLList, getPointDetail} from "../../../../api/target";

    export default {
        name: 'valueSync',
        props: {
            dialogParams: {
                type: Object
            }
        },
        data() {
            return {
                formData: {
                    indexX: '',
                    rectificationRangeId: '',
                    targetStandardId: '',
                    id: '',
                    name: '',
                    standardValue: '',
                },
                rangeList: [],
                rateList: [],
                targetStandardList: [],
                pointList: [],
            }
        },
        created() {
            this.rangeList = JSON.parse(sessionStorage.getItem('rangeList')) || []
        },
        methods: {
            submit() {
                if (this.formData.standardValue) {
                    this.dialogParams.data.targetValue = this.targetValue
                    this.$emit('changeStandardValue', this.formData);
                    this.dialogParams.visible = false
                } else {
                    this.$message.error('请选择所需的质控点的标准值！')
                }
            },
            close() {
                this.targetValue = '';
                this.dialogParams.visible = false
            },
            // 根据层面查询角度下拉值
            changeIndexX (val) {
                this.rateList = []
                this.formData.rectificationRangeId = ''
                if (this.rangeList) {
                    let temp = this.rangeList.filter(item => item.code === val)
                    if (temp.length) {
                        this.rateList = temp[0].rectificationRangeDTOList
                    }
                }
            },
            // 根据角度查询目标标准
            changeRate (range) {
                // console.log(range);
                this.targetStandardList = []
                this.targetStandardId = ''

                let params = {
                    indexX: this.formData.indexX,
                    rectificationRangeId: range
                }
                queryFormWorkAll(params).then(res => {
                    this.targetStandardList = res || []
                }).catch(() => {})
            },
            // 根据目标标准查询质控点
            changeTargetStandard (val) {
                this.pointList = []
                let params = {
                    targetStandardId: val
                }
                getPointALLList(params).then(res => {
                    this.pointList = res || []
                }).catch(() => {})
            },
            // 选择质控点的标准值
            changePoint (id) {
                getPointDetail(id).then(res => {
                    if (res) {
                        this.formData.name = res.name
                        this.formData.standardValue = res.standardValue
                    } else {
                        this.formData.standardValue = ''
                    }
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    .targetValue{
        display: flex;
        align-items: center;
        p{
            width: 80px;
        }
    }
</style>